/**
 *
 */

package com.person.modules.person.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.person.common.annotation.SysLog;
import com.person.common.utils.DateUtils;
import com.person.common.utils.PageUtils;
import com.person.common.utils.R;
import com.person.common.validator.ValidatorUtils;
import com.person.modules.person.entity.UserPlanEntity;
import com.person.modules.person.service.UserPlanService;
import com.person.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 个人计划
 */
@RestController
@RequestMapping("/person/plan")
public class UserPlanController extends AbstractController {
    @Autowired
    private UserPlanService userPlanService;

    /**
     * 所有个人计划列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("person:plan:list")
    @CrossOrigin
    public R list() {
        List<UserPlanEntity> list= userPlanService.getListByCompanyId(getUser().getCompanyId());
        return R.ok().put("list",list);

    }


    /**
     * 个人计划信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("person:plan:info")
    @ResponseBody
    @CrossOrigin
    public R info(@PathVariable("id") Long id) {
        UserPlanEntity plan = userPlanService.getById(id);
        plan.setCreateTime(DateUtils.currentTimeFormat());
        return R.ok().put("plan", plan);
    }

    /**
     * 保存个人计划
     */
    @SysLog("保存个人计划")
    @RequestMapping("/save")
    @RequiresPermissions("person:plan:save")
    @Transactional
    @CrossOrigin
    public R save( UserPlanEntity plan) {
        ValidatorUtils.validateEntity(plan);
        plan.setCompanyId(getCompanyId());
        userPlanService.save(plan);

        return R.ok();
    }

    /**
     * 修改个人计划
     */
    @SysLog("修改个人计划")
    @RequestMapping("/update")
    @RequiresPermissions("person:plan:update")
    @CrossOrigin
    public R update( UserPlanEntity plan) {
        ValidatorUtils.validateEntity(plan);
        plan.setUpdateTime(DateUtils.currentTimeFormat());
        plan.setCompanyId(getCompanyId());
        userPlanService.update(plan);

        return R.ok();
    }

    /**
     * 删除个人计划
     */
    @SysLog("删除个人计划")
    @RequestMapping("/delete")
    @RequiresPermissions("person:plan:delete")
    @CrossOrigin
    public R delete(@RequestBody Long[] ids) {
        userPlanService.deleteBatch(ids);
        return R.ok();
    }

}